#include "config.h"

#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

#include <clx/libECM.h>

#include "libms2sim.h"

typedef struct {
	double	mu;
	gsl_rng	*rng;
} arrival_t;

msl_arrival_t
msl_arrival_create(double mu, unsigned long seed)
{
	gsl_rng	*rng;
	arrival_t	*arrival;

	rng = gsl_rng_alloc(gsl_rng_default);
	if (rng == NULL)
		return NULL;
	arrival = (arrival_t *)ecm_malloc(sizeof(arrival_t));
	if (arrival == NULL) {
		gsl_rng_free(rng);
		return NULL;
	}
	if (seed > 0)
		gsl_rng_set(rng, seed);
	arrival->rng = rng;
	arrival->mu = mu;

	return arrival;
}

unsigned int
msl_arrival_get(msl_arrival_t msl_arrival)
{
	arrival_t	*arrival = (arrival_t *)msl_arrival;

	if (arrival == NULL)
		return 0;

	return gsl_ran_poisson(arrival->rng, arrival->mu);
}

void
msl_arrival_free(msl_arrival_t msl_arrival)
{
	arrival_t	*arrival = (arrival_t *)msl_arrival;

	if (arrival == NULL)
		return;

	gsl_rng_free(arrival->rng);
	ecm_free(arrival);
}
